<?php

namespace Acme\StaffBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Acme\StaffBundle\Entity\Staff;
use Acme\StaffBundle\Form\StaffType;
use Acme\StaffBundle\lib\AdminConfigJqGrid;
use Acme\StaffBundle\lib\AdminCategoryConfigJqGrid;
/**
 * Staff controller.
 *
 * @Route("/admin/admin_staff")
 */
class AdminController extends Controller {

  /**
   * Lists all Staff entities.
   *
   * @Route("/{firm_id}/firm/", name="admin_staff", defaults={"firm_id"=false })
   * @Route("/")
   * @Template()
   */
  public function indexAction(\Symfony\Component\HttpFoundation\Request $request) {
    $em = $this->getDoctrine()->getEntityManager();
    $adconfig = new AdminConfigJqGrid(array('EntityManager' => $em,
                'Repository' => 'AcmeStaffBundle:Staff'));
    $adconfig->getJqGrid()->findByKey('url')
            ->setValue($this->generateUrl('admin_get_staffs', array('firm_id'=> $request->get('firm_id'))));
    $adconfig->getJqGrid()->findByKey('editurl')
            ->setValue($this->generateUrl('admin_del_staff'));

    return array('params' => $adconfig->getJqGrid()->getProperties());
  }

  /**
   *
   * @Route("/get_staffs/{firm_id}/", name="admin_get_staffs", defaults={"firm_id"=false })
   *  @Route("/get_staffs/")
   */
  public function get_staffsAction(\Symfony\Component\HttpFoundation\Request $request) {
    if ($request->isXmlHttpRequest()) {
      $em = $this->getDoctrine()->getEntityManager();
      $adconfig = new AdminConfigJqGrid(array('EntityManager' => $em,
                  'Repository' => 'AcmeStaffBundle:Staff'));
      $adconfig->getJqGrid()->findByKey('url')
              ->setValue($this->generateUrl('admin_get_staffs'));
      $adconfig->getJqGrid()->findByKey('editurl')
              ->setValue($this->generateUrl('admin_del_staff'));
      $gj = new \Alex616\jqGridBundle\lib\GeneratorJqGrid($adconfig);
      $results = $gj->getResults($request, 'p');
      $response = new \Symfony\Component\HttpFoundation\Response($results, 200, array('Content-Type' => 'application/json'));
      return $response;
    }
    return new \Symfony\Component\HttpFoundation\Response('', 404);
  }

  /**
   * @Route("/del_staff", name="admin_del_staff")
   * 
   */
  public function deleteStaffAction(\Symfony\Component\HttpFoundation\Request $request) {
    if ($request->isXmlHttpRequest()) {
      $em = $this->getDoctrine()->getEntityManager();
      $adconfig = new AdminConfigJqGrid(array('EntityManager' => $em,
                  'Repository' => 'AcmeStaffBundle:Staff'));
      $adconfig->getJqGrid()->findByKey('url')
              ->setValue($this->generateUrl('admin_get_staffs'));
      $adconfig->getJqGrid()->findByKey('editurl')
              ->setValue($this->generateUrl('admin_del_staff'));
      $gj = new \Alex616\jqGridBundle\lib\GeneratorJqGrid($adconfig);
      $gj->removeObjects($request);
    }
    return new \Symfony\Component\HttpFoundation\Response('', 200);
  }

  /**
   * Lists all CategoryFirms entities.
   *
   * @Route("/staff_category", name="admin_staff_category")
   * @Template()
   */
  public function categoryAction() {
    $em = $this->getDoctrine()->getEntityManager();
    $adconfig = new AdminCategoryConfigJqGrid(array('EntityManager' => $em,
                'Repository' => 'AcmeStaffBundle:CategoryStaff'));
    $adconfig->getJqGrid()->findByKey('url')
            ->setValue($this->generateUrl('admin_get_staff_category'));
    $adconfig->getJqGrid()->findByKey('editurl')
            ->setValue($this->generateUrl('admin_action_staff_category'));

    return array('params' => $adconfig->getJqGrid()->getProperties());
  }

  /**
   *
   * @Route("/get_staff_category", name="admin_get_staff_category")
   */
  public function get_categoryAction(\Symfony\Component\HttpFoundation\Request $request) {
    if ($request->isXmlHttpRequest()) {
      $em = $this->getDoctrine()->getEntityManager();
      $adconfig = new AdminCategoryConfigJqGrid(array('EntityManager' => $em,
                  'Repository' => 'AcmeStaffBundle:CategoryStaff'));
      $adconfig->getJqGrid()->findByKey('url')
              ->setValue($this->generateUrl('admin_get_staff_category'));
      $adconfig->getJqGrid()->findByKey('editurl')
              ->setValue($this->generateUrl('admin_action_staff_category'));
      $gj = new \Alex616\jqGridBundle\lib\GeneratorJqGrid($adconfig);
      $results = $gj->getResults($request, 'p');
      $response = new \Symfony\Component\HttpFoundation\Response($results, 200, array('Content-Type' => 'application/json'));
      return $response;
    }
    return new \Symfony\Component\HttpFoundation\Response('', 404);
  }

  /**
   * @Route("/action_staff_category", name="admin_action_staff_category")
   * 
   */
  public function actionCategoryAction(\Symfony\Component\HttpFoundation\Request $request) {
    if ($request->isXmlHttpRequest()) {
      $oper = $request->get('oper');
      $id = $request->get('id');
      $em = $this->getDoctrine()->getEntityManager();
      switch ($oper) {
        case 'edit':
          $category = $em->getRepository('AcmeStaffBundle:CategoryStaff')->find($id);
          $category->setName($request->get('name'));
          $em->persist($category);
          break;
        case 'del':
          $ids = explode(',', $id);
          $repository = $em->getRepository('AcmeStaffBundle:CategoryStaff');
          if (count($ids) > 1) {
            $qb = $repository->createQueryBuilder('cf');
            $categoryes = $qb->where($qb->expr()->in('cf.id', '?1'))
                            ->setParameters(array(1 => $ids))
                            ->getQuery()->getResult();
            foreach ($categoryes as $category) {
              $em->remove($category);
            }
          }
          else {
            $category = $repository->find($ids[0]);
            $em->remove($category);
          }
          break;
        case 'add':
          $category = new \Acme\StaffBundle\Entity\CategoryStaff();
          $category->setName($request->get('name'));
          $em->persist($category);
          break;
        default:
          break;
      }
      $em->flush();
    }
    return new \Symfony\Component\HttpFoundation\Response('', 200);
  }

  /**
   * Displays a form to create a new Staff entity.
   *
   * @Route("/new", name="admin_staff_new")
   * @Template()
   */
  public function newAction() {
    $entity = new Staff();
    $form = $this->createForm(new StaffType(), $entity);

    return array(
        'entity' => $entity,
        'form' => $form->createView()
    );
  }

  /**
   * Creates a new Staff entity.
   *
   * @Route("/create", name="admin_staff_create")
   * @Method("post")
   * @Template("AcmeStaffBundle:Staff:new.html.twig")
   */
  public function createAction() {
    $entity = new Staff();
    $request = $this->getRequest();
    $form = $this->createForm(new StaffType(), $entity);
    $form->bindRequest($request);

    if ($form->isValid()) {
      $em = $this->getDoctrine()->getEntityManager();
      $em->persist($entity);
      $em->flush();

      return $this->redirect($this->generateUrl('admin_staff'));
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView()
    );
  }

  /**
   * Displays a form to edit an existing Staff entity.
   *
   * @Route("/{id}/edit", name="admin_staff_edit")
   * @Template()
   */
  public function editAction($id) {
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('AcmeStaffBundle:Staff')->find($id);

    if (!$entity) {
      throw $this->createNotFoundException('Unable to find Staff entity.');
    }

    $editForm = $this->createForm(new StaffType(), $entity);
    $deleteForm = $this->createDeleteForm($id);
    $comment = new \Acme\StaffBundle\Entity\Comment();
    $add_comment_form = $this->createForm(new \Acme\StaffBundle\Form\AddCommentForm(), $comment);
    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
        'add_comment_form' => $add_comment_form->createView(),
    );
  }

  /**
   * Edits an existing Staff entity.
   *
   * @Route("/{id}/update", name="admin_staff_update")
   * @Method("post")
   * @Template("AcmeStaffBundle:Staff:edit.html.twig")
   */
  public function updateAction($id) {
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('AcmeStaffBundle:Staff')->find($id);

    if (!$entity) {
      throw $this->createNotFoundException('Unable to find Staff entity.');
    }

    $editForm = $this->createForm(new StaffType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $request = $this->getRequest();

    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
      $em->persist($entity);
      $em->flush();

      return $this->redirect($this->generateUrl('admin_staff_edit', array('id' => $id)));
    }

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
  }

  /**
   * Deletes a Staff entity.
   *
   * @Route("/{id}/delete", name="admin_staff_delete")
   * @Method("post")
   */
  public function deleteAction($id) {
    $form = $this->createDeleteForm($id);
    $request = $this->getRequest();

    $form->bindRequest($request);

    if ($form->isValid()) {
      $em = $this->getDoctrine()->getEntityManager();
      $entity = $em->getRepository('AcmeStaffBundle:Staff')->find($id);

      if (!$entity) {
        throw $this->createNotFoundException('Unable to find Staff entity.');
      }

      $em->remove($entity);
      $em->flush();
    }

    return $this->redirect($this->generateUrl('admin_staff'));
  }

  private function createDeleteForm($id) {
    return $this->createFormBuilder(array('id' => $id))
                    ->add('id', 'hidden')
                    ->getForm()
    ;
  }

}
